Myblog::System.controllers :orders do

  # get :index, :map => '/foo/bar' do
  #   session[:foo] = 'bar'
  #   render 'index'
  # end

  # get :sample, :map => '/sample/url', :provides => [:any, :js] do
  #   case content_type
  #     when :js then ...
  #     else ...
  # end

  # get :foo, :with => :id do
  #   "Maps to url '/foo/#{params[:id]}'"
  # end

  # get '/example' do
  #   'Hello world!'
  # end

  get :index do
    if current_admin.is_seller == true
      @orders = Order.where(seller: current_admin.id)
      @manage_title = "我收到的订单"
    else
      @message = "您没有权限访问"
      redirect url(:login, :index), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe

    end

    if current_admin.is_super == true
      @orders = Order.all
      @manage_title = "所有订单"
    end


    render 'orders/index'
  end

  get :change_state, :with => :id do
    @order = Order.find(params[:id])
    @order.status = params[:state]
    if @order.update
      @order.status
    else
      0
    end
  end

  post :change_settlement_money do
    @order = Order.find(params[:order_id])
    @order.amount = params[:amount]
    if @order.is_close
      #当第一次填写金额之后，分利系统就会关闭
      @message = '交易金额更改成功，但是分享者利润不变'
      redirect url(:orders, :index), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end
    @order.is_close = true
    if @order.save!
      if @order.amount > 0
        # 判断订单是可以分帐的
        @node_level_one = @order.user.user_node.user_node
        if @node_level_one
          # 判断下单者是否有上级
          @node_level_one.user.balance.amount = @node_level_one.user.balance.amount + 341;
          @node_level_one.user.balance.save!
          @node_level_one.user.save!
          @bill = Bill.new
          @bill.cash_type = "收入"
          @bill.amount = 341
          @bill.info = "得到购买者：#{@order.user.username}的提成"
          @bill.user_to = @node_level_one.user.id
          @bill.balance = @node_level_one.user.balance
          @bill.status = 1
          @bill.save!
          if @node_level_one.user_node
            # 判断下单者的上级是否有上级
            @node_level_two = @node_level_one.user_node
            @node_level_two.user.balance.amount = @node_level_two.user.balance.amount + 159;
            @node_level_two.user.balance.save!
            @node_level_two.user.save!
            @bill = Bill.new
            @bill.cash_type = "收入"
            @bill.amount = 159
            @bill.info = "得到分享者：#{@node_level_one.user.username}的提成"
            @bill.user_to = @node_level_two.user.id
            @bill.balance = @node_level_two.user.balance
            @bill.status = 1
            @bill.save!
          else#若是没有上上级则另外的钱给网站管理员
            @root_user = User.where(mobile: "18820965455").first
            @root_user.balance.amount = @root_user.balance.amount + 159;
            @root_user.balance.save
            @root_user.save
            @bill = Bill.new
            @bill.cash_type = "收入"
            @bill.amount = 159
            @bill.info = "得到分享者：#{@node_level_one.user.username}的提成"
            @bill.user_to = @node_level_one.user.id
            @bill.balance = @root_user.balance
            @bill.status = 1
            @bill.save!

          end# 判断下单者的上级是否有上级
        else#若是没有上级，则所有一级分利给网站管理员
          @root_user = User.where(mobile: "18820965455").first
          @root_user.balance.amount = @root_user.balance.amount + 341;
          @root_user.balance.save
          @root_user.save
          @bill = Bill.new
          @bill.cash_type = "收入"
          @bill.amount = 341
          @bill.info = "得到购买者：#{@order.user.username}的提成"
          @bill.user_to = @order.user.id
          @bill.balance = @root_user.balance
          @bill.status = 1
          @bill.save!
        end# 判断下单者是否有上级

      else
        @message = '输入金额有误'
        redirect url(:orders, :index), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
      end# 判断订单是可以分帐的
      @message = '成交！'
      redirect url(:orders, :index), :success => @message.html_safe+'<i class="close icon"></i>'.html_safe
    else
        @message = '数据存储失败'
      redirect url(:orders, :index), :error => @message.html_safe+'<i class="close icon"></i>'.html_safe
    end# if @order.save!
  end

end
